package com.course_design.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.course_design.dao.StudentMapper;
import com.course_design.entity.Record;
import com.course_design.entity.Rule;
import com.course_design.entity.Student;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private RuleService ruleService;
	
	@Autowired
	private RecordService recordService;
	
	public PageInfo<Student> queryAllStudentWithClass(Integer classId,Integer pn,Integer size){
		PageHelper.startPage(pn, size);
		List<Student> students=studentMapper.selectByExampleWithPage(classId);
		PageInfo<Student> info=new PageInfo<>(students, 5);
		return info;
	}
	public void updateStudent(String[] studentNums, Integer ruleId,Integer classId) {
		
		List<String> list=Arrays.asList(studentNums);
		System.out.println(list);
		Rule rule=ruleService.queryRuleByRuleId(ruleId);
		studentMapper.updateStudentScore(list, rule.getDeal());
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createDate=format.format(new Date().getTime());
		for(String liString:list) {
			Record record=new Record();
			record.setClassinfo(classId);
			record.setCreatetime(new Date());
			record.setRuleinfo(ruleId);
			record.setStudentid(liString);
			System.out.println();
			System.out.println("11111");
			recordService.insertRecord(record);
		}
	}
	
	public void batch(List<Student> students) {
		System.out.println("------->"+students.size());
		for(Student student :students) {
			studentMapper.insert(student);
		}
	}
}
